#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;

int main()
{
	priority_queue<LL,vector<LL>,greater<LL>> q;
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		LL x;
		cin >> x;
		q.push(x);
	}
	
	LL ret = 0;
	while(q.size() > 1)
	{
		LL x = q.top(); q.pop();
		LL y = q.top(); q.pop();
		ret += x + y;
		q.push(x+y);
	}
	cout << ret;
	return 0;
}
